const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const notificationController = require('../controllers/notification.controller');

const router = new Router({ prefix: '/api/v1/notifications' });

/**
 * @swagger
 * tags:
 *   name: 消息通知
 *   description: 通知服务管理，包括邮件、短信和推送通知
 */

/**
 * @swagger
 * /api/v1/notifications/send:
 *   post:
 *     tags: [消息通知]
 *     summary: 发送通知
 *     description: 发送邮件、短信或推送通知
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - type
 *               - recipients
 *               - template
 *             properties:
 *               type:
 *                 type: string
 *                 enum: [email, sms, push]
 *                 description: 通知类型
 *               recipients:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 接收者列表（邮箱/手机号/设备标识）
 *               template:
 *                 type: string
 *                 description: 模板ID
 *               data:
 *                 type: object
 *                 description: 模板变量数据
 *     responses:
 *       201:
 *         description: 发送成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     result:
 *                       type: object
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/send', checkAuth, checkPermission('notification:write'), notificationController.send);

/**
 * @swagger
 * /api/v1/notifications/templates:
 *   get:
 *     tags: [消息通知]
 *     summary: 获取通知模板列表
 *     description: 获取所有可用的通知模板
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     templates:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                           name:
 *                             type: string
 *                           type:
 *                             type: string
 *                             enum: [email, sms, push]
 *                           content:
 *                             type: string
 *                           variables:
 *                             type: array
 *                             items:
 *                               type: string
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/templates', checkAuth, checkPermission('notification:read'), notificationController.getTemplates);

/**
 * @swagger
 * /api/v1/notifications/templates/{templateId}:
 *   get:
 *     tags: [消息通知]
 *     summary: 获取通知模板详情
 *     description: 获取指定ID的通知模板详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: templateId
 *         required: true
 *         schema:
 *           type: string
 *         description: 模板ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     template:
 *                       type: object
 *                       properties:
 *                         id:
 *                           type: string
 *                         name:
 *                           type: string
 *                         type:
 *                           type: string
 *                           enum: [email, sms, push]
 *                         content:
 *                           type: string
 *                         variables:
 *                           type: array
 *                           items:
 *                             type: string
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 模板不存在
 */
router.get('/templates/:templateId', checkAuth, checkPermission('notification:read'), notificationController.getTemplate);

/**
 * @swagger
 * /api/v1/notifications/templates:
 *   post:
 *     tags: [消息通知]
 *     summary: 创建通知模板
 *     description: 创建新的通知模板
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *               - content
 *             properties:
 *               name:
 *                 type: string
 *                 description: 模板名称
 *               type:
 *                 type: string
 *                 enum: [email, sms, push]
 *                 description: 通知类型
 *               subject:
 *                 type: string
 *                 description: 邮件主题（仅邮件类型需要）
 *               content:
 *                 type: string
 *                 description: 模板内容
 *               variables:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 模板变量列表
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     template:
 *                       type: object
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/templates', checkAuth, checkPermission('notification:write'), notificationController.createTemplate);

/**
 * @swagger
 * /api/v1/notifications/templates/{templateId}:
 *   put:
 *     tags: [消息通知]
 *     summary: 更新通知模板
 *     description: 更新指定的通知模板
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: templateId
 *         required: true
 *         schema:
 *           type: string
 *         description: 模板ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 模板名称
 *               type:
 *                 type: string
 *                 enum: [email, sms, push]
 *                 description: 通知类型
 *               subject:
 *                 type: string
 *                 description: 邮件主题（仅邮件类型需要）
 *               content:
 *                 type: string
 *                 description: 模板内容
 *               variables:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 模板变量列表
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     template:
 *                       type: object
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 模板不存在
 */
router.put('/templates/:templateId', checkAuth, checkPermission('notification:write'), notificationController.updateTemplate);

/**
 * @swagger
 * /api/v1/notifications/templates/{templateId}:
 *   delete:
 *     tags: [消息通知]
 *     summary: 删除通知模板
 *     description: 删除指定的通知模板
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: templateId
 *         required: true
 *         schema:
 *           type: string
 *         description: 模板ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 message:
 *                   type: string
 *                   example: "模板删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 模板不存在
 */
router.delete('/templates/:templateId', checkAuth, checkPermission('notification:write'), notificationController.deleteTemplate);

/**
 * @swagger
 * /api/v1/notifications/history:
 *   get:
 *     tags: [消息通知]
 *     summary: 获取通知历史记录
 *     description: 获取通知发送历史记录，支持分页和筛选
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [email, sms, push]
 *         description: 通知类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, sent, failed]
 *         description: 通知状态筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     history:
 *                       type: object
 *                       properties:
 *                         total:
 *                           type: integer
 *                         pages:
 *                           type: integer
 *                         current:
 *                           type: integer
 *                         records:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               id:
 *                                 type: string
 *                               type:
 *                                 type: string
 *                               status:
 *                                 type: string
 *                               createdAt:
 *                                 type: string
 *                                 format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/history', checkAuth, checkPermission('notification:read'), notificationController.getHistory);

/**
 * @swagger
 * /api/v1/notifications/{notificationId}/status:
 *   get:
 *     tags: [消息通知]
 *     summary: 获取通知状态
 *     description: 获取指定通知的发送状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: notificationId
 *         required: true
 *         schema:
 *           type: string
 *         description: 通知ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     status:
 *                       type: object
 *                       properties:
 *                         id:
 *                           type: string
 *                         status:
 *                           type: string
 *                           enum: [pending, sent, failed]
 *                         error:
 *                           type: string
 *                         createdAt:
 *                           type: string
 *                           format: date-time
 *                         completedAt:
 *                           type: string
 *                           format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 通知不存在
 */
router.get('/:notificationId/status', checkAuth, checkPermission('notification:read'), notificationController.getNotificationStatus);

/**
 * @swagger
 * /api/v1/notifications/{notificationId}/resend:
 *   post:
 *     tags: [消息通知]
 *     summary: 重新发送通知
 *     description: 重新发送指定的失败通知
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: notificationId
 *         required: true
 *         schema:
 *           type: string
 *         description: 通知ID
 *     responses:
 *       200:
 *         description: 重发成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     result:
 *                       type: object
 *       400:
 *         description: 重发失败
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 通知不存在
 */
router.post('/:notificationId/resend', checkAuth, checkPermission('notification:write'), notificationController.resend);

/**
 * @swagger
 * /api/v1/notifications/channels/{channel}/status:
 *   get:
 *     tags: [消息通知]
 *     summary: 获取通知渠道状态
 *     description: 获取指定通知渠道的运行状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: channel
 *         required: true
 *         schema:
 *           type: string
 *           enum: [email, sms, push]
 *         description: 通知渠道
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "成功"
 *                 data:
 *                   type: object
 *                   properties:
 *                     status:
 *                       type: object
 *                       properties:
 *                         active:
 *                           type: boolean
 *                         queueSize:
 *                           type: integer
 *                         lastError:
 *                           type: string
 *                         lastSuccessful:
 *                           type: string
 *                           format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/channels/:channel/status', checkAuth, checkPermission('notification:read'), notificationController.getChannelStatus);

module.exports = router; 